#import "CircleProgressView.h"

@implementation CircleProgressView

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    [BgGrayColor setStroke];
    CGContextSetLineWidth(context, 10.0);
    
    CGRect circleRect = CGRectInset(self.bounds, 10, 10);
    CGContextAddEllipseInRect(context, circleRect);
    CGContextStrokePath(context);
    
    CGContextBeginPath(context);
    CGContextSetLineWidth(context, 10.0);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGFloat startAngle = M_PI_2;
    CGFloat endAngle = startAngle + (2 * M_PI * self.progress);
    CGContextAddArc(context, self.bounds.size.width / 2, self.bounds.size.height / 2, (self.bounds.size.width - 20) / 2, startAngle, endAngle, 0);
    [!self.color?colorWithHex(0xFBE307):self.color setStroke];
    CGContextStrokePath(context);
    
//    if (self.progress > 0) {
        CGFloat radius = (self.bounds.size.width - 20) / 2;
        CGFloat x = self.bounds.size.width / 2 + cos(startAngle + (2 * M_PI * self.progress)) * radius;
        CGFloat y = self.bounds.size.height / 2 + sin(startAngle + (2 * M_PI * self.progress)) * radius;
        CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
        CGContextFillEllipseInRect(context, CGRectMake(x - 2, y - 2, 4, 4));
//    }
}

-(void)setProgress:(CGFloat)progress{
    _progress = progress;
    [self setNeedsDisplay];
}

@end
